
using QdbTool.Tests.Core;
using Mono.Cecil;

namespace QdbTool.Tests.TA
{
	class TAWarningOnNonPrivateFieldsTestCase : TAOutputListenerTestCaseBase
	{
		public void TestWarningOnNonPrivateFields()
		{
			AssemblyDefinition assembly = GenerateAssembly("TAClassWithNonPrivateFieldsSubject");
			InstrumentAndAssert(
				AssemblyPath(assembly), 
				true,
				ResultFor("TAClassWithPublicFieldSubject", "value"),
				ResultFor("TAClassWithProtectedFieldSubject", "value"));
		}

		public void TestNoWarningsForNonInstrumentedClasses()
		{
			CompilationServices.ExtraParameters.Using(
				"/d:" + TargetPlatformDefinition(),
				delegate
					{
						AssemblyDefinition assembly = GenerateAssembly("TANoFalsePositiveWarningsForNonPrivateFields");
						InstrumentAndAssert(
								AssemblyPath(assembly),
								"-v -ta -by-name:TAFilteredOut -not",
								true,
								ResultFor("TAMixOfPersistentAndNoPersistentFields", "_persistentInt"));
					});
		}

		private static string AssemblyPath(AssemblyDefinition assembly)
		{
			return assembly.MainModule.Image.FileInformation.FullName;
		}

		private static string ResultFor(string typeName, string fieldName)
		{
			return string.Format("Found non-private field '{0}' in instrumented type '{1}'", fieldName, typeName);
		}

		private static string TargetPlatformDefinition()
		{
#if NET_2_0
			return "NET_2_0";
#else
			return "NET_3_5";
#endif
		}
	}
}
